package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodCompanyFile;
import com.ruoyi.food.mapper.OpmFoodCompanyFileMapper;
import com.ruoyi.food.service.IOpmFoodCompanyFileService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业安全档案Service业务层处理
 *
 * @author cjie
 * @date 2021-08-02
 */
@Service
public class OpmFoodCompanyFileServiceImpl extends LinkFileServiceImpl implements IOpmFoodCompanyFileService {
    @Autowired
    private OpmFoodCompanyFileMapper opmFoodCompanyFileMapper;

    /**
     * 查询企业安全档案
     *
     * @param id 企业安全档案ID
     * @return 企业安全档案
     */
    @Override
    public OpmFoodCompanyFile selectOpmFoodCompanyFileById(Long id) {
        OpmFoodCompanyFile result = opmFoodCompanyFileMapper.selectOpmFoodCompanyFileById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询企业安全档案列表
     *
     * @param opmFoodCompanyFile 企业安全档案
     * @return 企业安全档案
     */
    @Override
    public List<OpmFoodCompanyFile> selectOpmFoodCompanyFileList(OpmFoodCompanyFile opmFoodCompanyFile) {
        return opmFoodCompanyFileMapper.selectOpmFoodCompanyFileList(opmFoodCompanyFile);
    }

    /**
     * 新增企业安全档案
     *
     * @param opmFoodCompanyFile 企业安全档案
     * @return 结果
     */
    @Override
    public int insertOpmFoodCompanyFile(OpmFoodCompanyFile opmFoodCompanyFile) {
        opmFoodCompanyFile.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodCompanyFile.setCreateTime(DateUtils.getNowDate());
        opmFoodCompanyFile.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodCompanyFile.getFileIdList(), opmFoodCompanyFile.getId().toString());
        return opmFoodCompanyFileMapper.insertOpmFoodCompanyFile(opmFoodCompanyFile);
    }

    /**
     * 修改企业安全档案
     *
     * @param opmFoodCompanyFile 企业安全档案
     * @return 结果
     */
    @Override
    public int updateOpmFoodCompanyFile(OpmFoodCompanyFile opmFoodCompanyFile) {
        opmFoodCompanyFile.setUpdateTime(DateUtils.getNowDate());
        opmFoodCompanyFile.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodCompanyFile.getFileIdList(), opmFoodCompanyFile.getId().toString());
        return opmFoodCompanyFileMapper.updateOpmFoodCompanyFile(opmFoodCompanyFile);
    }

    /**
     * 批量删除企业安全档案
     *
     * @param ids 需要删除的企业安全档案ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodCompanyFileByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodCompanyFileMapper.deleteOpmFoodCompanyFileByIds(ids);
    }

    /**
     * 删除企业安全档案信息
     *
     * @param id 企业安全档案ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodCompanyFileById(Long id) {
        return opmFoodCompanyFileMapper.deleteOpmFoodCompanyFileById(id);
    }
}
